﻿using System;
using System.Net.Http;
using IdentityModel;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ERP.BlazorWams.Menus;
using Volo.Abp.Autofac.WebAssembly;
using Volo.Abp.Modularity;
using ERP.Abp.TenantManagement.Blazor.WebAssembly.AntDesignUI;
using ERP.Abp.SettingManagement.Blazor.WebAssembly.AntDesignUI;
using Volo.Abp.AspNetCore.Components.Messages;
using Volo.Abp.AspNetCore.Components.Notifications;
using Volo.Abp.AspNetCore.Components.Progression;

using AntDesign.ProLayout;
using Volo.Abp.UI.Navigation;
using ERP.Blazor.Services;
using Volo.Abp.Features;
using ERP.Abp.AspnetCore.Components.WebAssembly.AntDesignTheme;
using ERP.Abp.Blazor.WebAssembly.AntDesignUI;
using ERP.Abp.AspnetCore.Components.Web.AntDesignTheme.Routing;
using ERP.Abp.AntDesignUI;
using Volo.Abp.AutoMapper;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using ERP.Workflow.Designer.BlazorUI;
using ERP.Abp.FeatureManagement.Blazor.WebAssembly.AntDesignUI;

namespace ERP.Blazor;
[DependsOn(
    typeof(AbpAutofacWebAssemblyModule),
    typeof(ERPHttpApiClientModule),
   typeof(AbpAspNetCoreComponentsWebAssemblyAntDesignThemeModule),
   typeof(AbpAutoMapperModule),
   typeof(AbpIdentityBlazorWebAssemblyAntDesignModule),
    typeof(AbpTenantManagementBlazorWebAssemblyAntDesignModule),
  typeof(AbpSettingManagementBlazorWebAssemblyAntDesignModule),
    
    typeof(AbpFeatureManagementBlazorWebAssemblyAntDesignModule),
    typeof(WorkflowDesignerBlazorUIModule)
     
)]
public class ERPBlazorModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {

        var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>();
        var builder = context.Services.GetSingletonInstance<WebAssemblyHostBuilder>();
  
        ConfigureAuthentication(builder);
        ConfigureHttpClient(context, environment);
        ConfigureBlazorise(context);
        ConfigureRouter(context);
        ConfigureMenu(context);
        ConfigureAutoMapper(context);
        ConfigureUI(builder);
    }
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
      
        base.PreConfigureServices(context);
    }
   
    private void ConfigureRouter(ServiceConfigurationContext context)
    {
        Configure<AbpRouterOptions>(options =>
        {
            options.AppAssembly = typeof(ERPBlazorModule).Assembly;
        });
    }
    private void ConfigureMenu(ServiceConfigurationContext context)
    {
        Configure<AbpNavigationOptions>(options =>
        {
            options.MenuContributors.Add(new ERPMenuContributor(context.Services.GetConfiguration()));
        });
    }

    private void ConfigureBlazorise(ServiceConfigurationContext context)
    {
        //context.Services
        //   .AddBootstrap5Providers()
        //  .AddFontAwesomeIcons();
    }
    private static void ConfigureAuthentication(WebAssemblyHostBuilder builder)
    {
        builder.Services.AddOidcAuthentication(options =>
        {
            builder.Configuration.Bind("AuthServer", options.ProviderOptions);
            options.UserOptions.RoleClaim = JwtClaimTypes.Role;
            options.ProviderOptions.DefaultScopes.Add("ERP");
            options.ProviderOptions.DefaultScopes.Add("role");
            options.ProviderOptions.DefaultScopes.Add("email");
            options.ProviderOptions.DefaultScopes.Add("phone");
        });
    }

    private static void ConfigureUI(WebAssemblyHostBuilder builder)
    {
        builder.Services.AddAntDesign();
        builder.Services.Configure<ProSettings>(builder.Configuration.GetSection("ProSettings"));
        builder.RootComponents.Add<ERP.BlazorWams.AppAnt>("#ApplicationContainer");
        builder.Services.AddWorkflow();


        builder.Services.AddScoped<IChartService, ChartService>();
        builder.Services.AddScoped<IProjectService, ProjectService>();
        builder.Services.AddScoped<IUserService, UserService>();
        builder.Services.AddScoped<IAccountService, AccountService>();
        builder.Services.AddScoped<IProfileService, ProfileService>();
    }

    private static void ConfigureHttpClient(ServiceConfigurationContext context, IWebAssemblyHostEnvironment environment)
    {
        context.Services.AddTransient(sp => new HttpClient
        {
            BaseAddress = new Uri(environment.BaseAddress)
        });
    }

    private void ConfigureAutoMapper(ServiceConfigurationContext context)
    {
        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<ERPBlazorModule>();
        });
    }
}
